package com.util;

/**
 * 方法参数获取类
 * @author seven
 */
public class ParamUtil {

    private static String arr = "[";
    private static String arrStr = "[]";
    /**
     * 获取参数类型
     * @param type
     * @return
     */
    public static String getParam(String type){
        String result = "";
        int arrLength=0;
        if(type.startsWith(arr)){
            arrLength  =  type.lastIndexOf("[")-type.indexOf("[");
            type = type.substring(type.lastIndexOf("[")+1);
        }
        String str = type.substring(0,1);
        switch (str) {
            case "B":
                result = "byte";
                break;
            case "C":
                result = "char";
                break;
            case "D":
                result = "double";
                break;
            case "F":
                result = "float";
                break;
            case "I":
                result = "int";
                break;
            case "J":
                result = "long";
                break;
            case "S":
                result = "short";
                break;
            case "Z":
                result = "boolean";
                break;
            case "V":
                result = "void";
                break;
            default:
                result = type.substring(type.lastIndexOf("/") + 1);
                break;
        }
        if(arrLength >0){
            for (int i=0;i<=arrLength;i++){
                result+=arrStr;
            }
        }
        return result;
    }

    /**
     * 方法返回值
     * @param type
     * @return
     */
    public static String getMethodResult(String type){
        String result = "";
        String str = type.substring(type.indexOf(")")+1);
        int arrLength=0;
        if(str.startsWith(arr)){
            arrLength  =  type.lastIndexOf("[")-type.indexOf("[");
            str = str.substring(str.lastIndexOf("[")+1);
        }
        result = getType(str);
        if(arrLength >0){
            for (int i=0;i<=arrLength;i++){
                result+="[]";
            }
        }
        return result;
    }

    /**
     * 获取类型
     * @param str
     * @return
     */
    private static String getType(String str){
        switch (str) {
            case "B":
                 return "byte";
            case "C":
                return "char";
            case "D":
                return "double";
            case "F":
                return "float";
            case "I":
                return "int";
            case "J":
                return "long";
            case "S":
                return "short";
            case "Z":
                return "boolean";
            case "V":
                return "void";
            default:
                return str.substring(str.lastIndexOf("/") + 1);

        }
    }
}
